package cn.kailangtu.dataspot.data;

import android.util.Log;

import java.text.DecimalFormat;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import cn.kailangtu.commontools.CommonUtils;

public class NewDataContainer {
    Map<Integer,byte[]> container;


    private static NewDataContainer instance = null;

    public synchronized static NewDataContainer getInstance(){
        if(instance == null)
        {
            instance = new NewDataContainer();
        }

        return instance;
    }


    private NewDataContainer()
    {
        container = new ConcurrentHashMap<>();
    }


    public void setMultiValue(int beginAddress,byte[] data)
    {

        if(data != null )
        {
            int count = data.length / 2;
            for(int i = 0;i< count;i++){
                int index = 3+2 *i;
                if(data.length > index+1) {
                    setNewValue(beginAddress + i, new byte[]{data[index], data[index + 1]});
                }
            }
        }
    }

    public void setNewValue(int address,byte[] data)
    {
       // Log.d("SEND AND RECEIVE","SET NEW VALUE,ADDRESS:"+address);
        byte[] value = null;
        if(container.containsKey(address))
        {
            value = container.get(address);
        }
        else{
            value = new byte[2];
            container.put(address,value);
        }
        if(data.length >=2)
        {
            System.arraycopy(data,0,value,0,2);
        }
        else
        {
            System.arraycopy(data,0,value,0,data.length);
        }
    }


    public byte[] getData(int address)
    {
        if(container.containsKey(address))
        {
            return container.get(address);
        }
        else{
            byte[] newData =  new byte[2];
            container.put(address,newData);
            return newData;
        }
    }

    public int getData16(int address)
    {
        int result = 0;
        if(container.containsKey(address))
        {
            byte[] current = container.get(address);
            result =  CommonUtils.combineBytesToInt(current[0],current[1]);
        }


        return result;
    }

    public int getData32(int address)
    {
        int result = 0;

        if(container.containsKey(address) && container.containsKey(address+1))
        {
            byte[] current1 = container.get(address);
            byte[] current2 = container.get(address+1);
            result = CommonUtils.byteArrayToInt(new byte[]{current1[0],current1[1],current2[0],current2[1]});
        }



        return result;
    }

    DecimalFormat df = new DecimalFormat("#.##");
    DecimalFormat df0 = new DecimalFormat("#");

    public String get正有功电能()
    {
        return df.format(get有功电能1() * get电压选择1() * get互感器1() /(1000 * 3600));
    }

    public String get负有功电能()
    {
        return df.format(get有功电能2() * get电压选择1() * get互感器1() /(1000 * 3600));
    }

    public String get正无功电能(){
        return df.format(get无功电能1() * get电压选择1() * get互感器1() /(1000 * 3600));
    }

    public String get负无功电能(){
        return df.format(get无功电能2() * get电压选择1() * get互感器1() /(1000 * 3600));
    }

    public String get有功率显示(){
        return df0.format(get有功率() / 10000 * get电压选择1() * get互感器1() *3 / 1000);
    }

    public String get无功率显示(){
        return df0.format( get无功率() / 10000 * get电压选择1() * get互感器1() *3 /1000);
    }

    public String get视在功率显示(){
        return df0.format( get视在功率() / 10000 * get电压选择1() * get互感器1() *3 / 1000);
    }

    public String get功率因素(){

        return df.format( new Double(getData16(14)) / 10000);


    }

    public double get互感器1()
    {
        int 互感器 =  HighData.getInstance().getValue(4502);
        double 互感器1 = 630;
        switch(互感器)
        {
            case 0:
                互感器1 = new Double(630)*0.96;
                break;
            case 1:
                互感器1 = new Double(1000)*0.96;
                break;
            case 2:
                互感器1 = new Double(1250)*0.96;
                break;
            case 3:
                互感器1 = new Double(1600)*0.96;
                break;
            case 4:
                互感器1 = new Double(2000)*0.96;
                break;
        }

        return 互感器1;

    }

    public double get电压选择1()
    {
        int 系统电压 = HighData.getInstance().getValue(4500);
        double 电压选择1 = 3300*1.732*0.9648;

        switch(系统电压)
        {
            case 1:
                电压选择1=1140*1.732*0.9648;
                break;
            case 2:
                电压选择1=1140*1.732*0.9648;
                break;
            case 3:
                电压选择1=1140*1.732*0.9648;
                break;
        }

        return 电压选择1;
    }

    public double get有功电能1()
    {
        return new Double(getData32(16));
    }

    public double get无功电能1()
    {
        return new Double(getData32(18));
    }

    public double get有功电能2()
    {
        return new Double(getData32(20));
    }

    public double get无功电能2()
    {
        return new Double(getData32(22));
    }

    public double get有功率()
    {
        return new Double(getData16(12));
    }

    public double get无功率()
    {
        return new Double( getData16(13));
    }

    public double get视在功率()
    {
        return new Double(getData16(30));
    }

    public double get电压A()
    {
        return new Double(getData16(0))  / 10000 * get电压选择1();
    }

    public double get电压B()
    {
        return new Double(getData16(1)) / 10000 *  get电压选择1();
    }

    public double get电压C()
    {
        return new Double( getData16(2)) / 10000 *  get电压选择1();
    }

    public double get电流A(){
        return new Double(getData16(3)) / 10000 * get互感器1();
    }

    public double get电流B(){
        return new Double( getData16(4)) / 10000 * get互感器1();
    }

    public double get电流C(){
        return new Double( getData16(5)) / 10000 * get互感器1();
    }

    public double getA相功率因素()
    {
        return new Double(getData16(6));
    }

    public double getB相功率因素()
    {
        return new Double(getData16(7));
    }

    public double getC相功率因素()
    {
        return new Double(getData16(8));
    }
}
